use std::{io::Read, fs::File};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct ConfServer {
    // 匹配模式 默认正则表达式
    #[serde(default = "default_rule_mode", rename = "rule-mode")]
    pub rule_mode: String,
    #[serde(default = "default_log")]
    pub log: String,
    #[serde(default = "default_socket_port", rename = "port-socket")]
    pub port_socket: u16,
    #[serde(default = "default_http_port", rename = "port-http")]
    pub port_http: u16,
    #[serde(default = "default_proxy_retry", rename = "proxy.retry")]
    pub proxy_retry: u8,
}

fn default_rule_mode() -> String {
    String::from("regx")
}

fn default_log() -> String {
    String::from("/var/log/nrgrid/nrgrid-server.log")
}

fn default_socket_port() -> u16 {
    9432_u16
}

fn default_http_port() -> u16 {
    9532_u16
}

fn default_proxy_retry() -> u8 {
    3_u8
}

impl ConfServer {
    pub fn get_conf() -> Result<ConfServer, serde_yaml::Error> {
        let mut conf_file = File::open("./conf/server.yml").unwrap();
        let mut conf_str = String::new();
        conf_file.read_to_string(&mut conf_str).unwrap();
        serde_yaml::from_str(&conf_str)
    }
}